package weekimwee.cn.x5nightdemo

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {
    private lateinit var pop: ColorPop
    private lateinit var fontPop:FontSizePop
    private var str =""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        editor.focusEditor()
        small.setOnClickListener {
            fontPop.showAsDropDown(small, 0, -color.height * 2)
        }
        line.setOnClickListener {
            editor.setUnderline()
        }
        thick.setOnClickListener {
            editor.setBold()
        }
        image.setOnClickListener {
            editor.insertImage("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2329760110,2568888454&fm=27&gp=0.jpg","pic")
        }
        video.setOnClickListener {

        }
        url.setOnClickListener {
            startActivityForResult(Intent(this@MainActivity,LinkActivity::class.java),1001)
        }
        color.setOnClickListener {
            pop.showAsDropDown(color, 0, -color.height * 2)
        }
        yl.setOnClickListener {
            startActivity(Intent(this@MainActivity,PreviewActivity::class.java).putExtra("str",str))
        }
        pop = ColorPop(this, View.OnClickListener { v ->
            pop.dismiss()
            when (v.id) {
                R.id.red -> {
                    editor.setTextColor(Color.BLACK)
                }
                R.id.yellow -> {
                    editor.setTextColor(Color.YELLOW)
                }
                R.id.green -> {
                    editor.setTextColor(Color.GREEN)
                }
            }
        })
        pop.isOutsideTouchable = true
        fontPop = FontSizePop(this,View.OnClickListener { v->
            fontPop.dismiss()
            when(v.id){
                R.id.xiao->editor.setFontSize(3)
                R.id.zhong->editor.setFontSize(5)
                R.id.da->editor.setFontSize(7)
            }
        }).apply {
            isOutsideTouchable = true
        }
        editor.setOnTextChangeListener {
            text ->
            str = text
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode!= Activity.RESULT_OK) return
        if(requestCode ==1001){
            data?.let {
                val title = it.getStringExtra("title")
                val url = it.getStringExtra("url")
                editor.insertLink(url,title)
            }
        }
    }
}
